/**
 * create by Wang.Pang 20180122
 * 获取表单域的键值对
 * @param element 表单域
 * @returns {Array} 键值对
 */
function getElementKeyValue(element) {   
    switch (element.type.toLowerCase()) {    
    case 'checkbox':
    	return inputSelector(element);
    case 'radio': 
    	return inputSelector(element);
    default:
    	return [element.name, element.value];
  }    
  return null;    
} 

/**
 * create by Wang.Pang 20180122
 * 获取复选框和单选按钮 选中的键值对
 * @param element 表单域
 * @returns {Array} 键值对
 */
function inputSelector(element) {
	if (element.checked){
		return [ element.name, element.value ];
	}else{
		return null;
	}
}

/**
 * create by Wang.Pang 20180122
 * 获取表单中域元素
 * @param formId 表单id
 * @returns {Array} 元素列表
 */
function getElements(formId) {    
    var form = document.getElementById(formId);    
    var elements = new Array();    
    getFormElementByType(form, 'input',elements);
    getFormElementByType(form, 'textarea',elements);
    getFormElementByType(form, 'select',elements);
    return elements;    
}

/**
 * 将表单域的值存放到数组里面
 * create by Wang.Pang 20180123
 * @param form 表单对象
 * @param type 域类型
 * @param elements 汇总数组
 * @returns
 */
function getFormElementByType(form, type,elements) {
	var tagElements = form.getElementsByTagName(type);    
    for (var j = 0; j < tagElements.length; j++){ 
    	 var value = getElementKeyValue(tagElements[j])
    	 if(value){
    		 elements.push(value); 
    	 }
    }
    return elements;
} 

/**
 * 将包含键值对的二维数组转换成json
 * create by Wang.Pang 20180123
 * @param array2D
 * @returns {String}
 */
function array2DToJsonStr(array2D){
	var jsonStr = "{";
	for (var i = 0; i < array2D.length; i++) {
		jsonStr += "\"" + array2D[i][0] + "\":\"" + array2D[i][1] + "\"";
		if(i != array2D.length -1){
			jsonStr +=",";
		}
	}
	jsonStr +="}";
	return jsonStr;
}

/**
 * 将包含键值对的二维数组转换为url参数
 * create by Wang.Pang 20180123
 * @param array2D
 * @returns {String}
 */
function array2DToUrlStr(array2D){
	var jsonStr = "";
	for (var i = 0; i < array2D.length; i++) {
		jsonStr += encodeURIComponent(array2D[i][0]) + "=" + encodeURIComponent(array2D[i][1]);
		if(i != array2D.length -1){
			jsonStr +="&";
		}
	}
	return jsonStr;
}

/**
 * 根据数组里面标签域的id 获取它的key-value 放到返回数组中
 * create by Wang.Pang 20180123
 * @param idArray 存放标签域id的数组
 * @returns {Array} 转换之后的key-value
 */
function idArrayToArray2D(idArray){
	var array2D = new Array();
	for (var i = 0; i < idArray.length; i++) {
		var element = document.getElementById(idArray[i]);
		if(element){
			var map = getElementKeyValue(element);
			if(map){
				array2D.push(map);
			}
		}
	}
	return array2D;
} 

/**
 * 根据数组里面标签域的name 获取它的key-value 放到返回数组中
 * create by Wang.Pang 20180123
 * @param nameArray 存放标签域name的数组
 * @returns {Array} 转换之后的key-value
 */
function nameArrayToArray2D(nameArray){
	var array2D = new Array();
	for (var i = 0; i < nameArray.length; i++) {
		var element = document.getElementsByName(nameArray[i]);
		for (var j = 0; j < element.length; j++) {
			var sub = element[j];
			if(sub){
				var map = getElementKeyValue(sub);
				if(map){
					array2D.push(map);
				}
			}
		}
	}
	return array2D;
}